"""
股票数据模型定义
"""
import datetime
from sqlalchemy import Column, Integer, String, Float, DateTime, Date, BigInteger, Text, Index

from database.db_engine import Base

class StockBasicInfo(Base):
    """股票基础信息表"""
    __tablename__ = "stock_basic_info"
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
    ts_code = Column(String(10), nullable=False, unique=True, comment="股票代码（带交易所标识）")
    stock_code = Column(String(6), nullable=False, comment="股票代码")
    name = Column(String(50), nullable=False, comment="股票名称")
    area = Column(String(50), nullable=True, comment="所在地域")
    industry = Column(String(50), nullable=True, comment="所属行业")
    market = Column(String(20), nullable=True, comment="市场类型")
    list_date = Column(Date, nullable=True, comment="上市日期")
    total_share = Column(Float, nullable=True, comment="总股本(亿)")
    circulating_share = Column(Float, nullable=True, comment="流通股本(亿)")
    total_capital = Column(Float, nullable=True, comment="总市值(亿)")
    circulating_capital = Column(Float, nullable=True, comment="流通市值(亿)")
    pe = Column(Float, nullable=True, comment="市盈率")
    pb = Column(Float, nullable=True, comment="市净率")
    status = Column(String(1), nullable=True, comment="股票状态(L上市 D退市 P暂停上市)")
    update_time = Column(DateTime, nullable=False, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment="更新时间")
    
    __table_args__ = (
        Index('idx_ts_code', 'ts_code'),
        Index('idx_stock_code', 'stock_code'),
    )
    
    def __repr__(self):
        return f"<StockBasicInfo(id={self.id}, ts_code='{self.ts_code}', name='{self.name}')>"


class StockDailyData(Base):
    """股票每日交易数据表"""
    __tablename__ = "stock_daily_data"
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
    ts_code = Column(String(10), nullable=False, comment="股票代码（带交易所标识）")
    stock_code = Column(String(6), nullable=False, comment="股票代码")
    trade_date = Column(Date, nullable=False, comment="交易日期")
    open = Column(Float, nullable=True, comment="开盘价")
    high = Column(Float, nullable=True, comment="最高价")
    low = Column(Float, nullable=True, comment="最低价")
    close = Column(Float, nullable=True, comment="收盘价")
    pre_close = Column(Float, nullable=True, comment="昨收价")
    change = Column(Float, nullable=True, comment="涨跌额")
    pct_change = Column(Float, nullable=True, comment="涨跌幅(%)")
    volume = Column(BigInteger, nullable=True, comment="成交量(手)")
    amount = Column(Float, nullable=True, comment="成交额(千元)")
    turnover_rate = Column(Float, nullable=True, comment="换手率(%)")
    update_time = Column(DateTime, nullable=False, default=datetime.datetime.now, comment="更新时间")
    
    __table_args__ = (
        Index('idx_ts_code_trade_date', 'ts_code', 'trade_date', unique=True),
        Index('idx_trade_date', 'trade_date'),
        Index('idx_stock_code', 'stock_code'),
    )
    
    def __repr__(self):
        return f"<StockDailyData(id={self.id}, ts_code='{self.ts_code}', trade_date='{self.trade_date}')>"


class StockRealtimeData(Base):
    """股票实时交易数据表"""
    __tablename__ = "stock_realtime_data"
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
    ts_code = Column(String(10), nullable=False, comment="股票代码（带交易所标识）")
    stock_code = Column(String(6), nullable=False, comment="股票代码")
    trade_time = Column(DateTime, nullable=False, comment="交易时间")
    price = Column(Float, nullable=True, comment="当前价格")
    change = Column(Float, nullable=True, comment="涨跌额")
    pct_change = Column(Float, nullable=True, comment="涨跌幅(%)")
    volume = Column(BigInteger, nullable=True, comment="成交量(手)")
    amount = Column(Float, nullable=True, comment="成交额(千元)")
    bid_price = Column(Text, nullable=True, comment="买盘价格(档位:价格,量)")
    ask_price = Column(Text, nullable=True, comment="卖盘价格(档位:价格,量)")
    update_time = Column(DateTime, nullable=False, default=datetime.datetime.now, comment="更新时间")
    
    __table_args__ = (
        Index('idx_ts_code_trade_time', 'ts_code', 'trade_time', unique=True),
        Index('idx_trade_time', 'trade_time'),
        Index('idx_stock_code', 'stock_code'),
    )
    
    def __repr__(self):
        return f"<StockRealtimeData(id={self.id}, ts_code='{self.ts_code}', trade_time='{self.trade_time}')>" 